package com.webserver.core;

import com.webserver.http.HttpServletRequest;

import java.io.*;
import java.net.Socket;
import java.security.spec.RSAOtherPrimeInfo;
import java.util.HashMap;
import java.util.Map;

/**
 * 线程任务
 * 负责与特定的客户端进行HTTP交互.
 */
public class ClientHandler implements Runnable {
    //拿到通过HTTp协议过来的流socket
    private Socket socket;

    //构造器初始化数据
    public ClientHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //1.接收请求
            HttpServletRequest request = new HttpServletRequest(socket);
//            //2.处理请求
//            String path = request.getUri();
//
//            //3.响应请求
//            File file = new File("./webapps" + path);//要将./webapps/myweb/index.html响应给浏览器
//            OutputStream out = socket.getOutputStream();
//            //发送状态行，此时的状态行是写死了的，便于初期学习以及理解
//
//            //3.1发送状态行
//            String line = "Http/1.1 200 ok";
//            //使用命名字符集将该字符串编码为字节序列，并将结果存储到一个新的字节数组中。
//            //字符串->字节序列->字节数组
//            //下面没写byte[]和String的原因是：发送了就没了，被GC线程回收了
//            // 对象又重新指向了line和byte[]数组
//            byte[] data = line.getBytes("ISO8859-1");
//            out.write(data);
//            out.write(13);//发送一个回车符
//            out.write(10);//发送一个换行符
//
//            //3.2发送响应头
//            line = "Content-Type: text/html";
//            data = line.getBytes("ISO8859-1");
//            out.write(data);
//            out.write(13);
//            out.write(10);
//            line = "Content-Length: "+file.length();
//            data = line.getBytes("ISO8859-1");
//            out.write(data);
//            out.write(13);
//            out.write(10);
//            //单独发送CRLF表示响应头部分发送完毕
//            out.write(13);
//            out.write(10);
//
//            //3.3发送响应正文
//            //文件输入流，读取从new文件开始到单独发送CRLF的内容给fis对象
//            FileInputStream fis = new FileInputStream(file);
//            int len;
//            byte[] buf = new byte[1024 * 10];
//            while ((len=fis.read()) != -1){
//                out.write(buf,0,len);
//            }
//            System.out.println("响应发送完毕！");


            //2处理请求
            String path = request.getUri();
            //3.1响应消息行
            File file = new File("./webapps" + path);
            OutputStream out = socket.getOutputStream();
            //固定消息行
            String line = "Http/1.1 200 ok";
            byte[] data = line.getBytes("ISO8859-1");
            out.write(data);
            out.write(13);
            out.write(10);

            //3.2响应消息头
            line = "Content-Type: text/html";
            data = line.getBytes("ISO8859-1");
            out.write(data);
            out.write(13);
            out.write(10);

            line = "Content-Length: "+file.length();
            data = line.getBytes("ISO8859-1");
            out.write(data);
            out.write(13);
            out.write(10);

            out.write(13);
            out.write(10);

            //3.3发送消息正文
            FileInputStream fis = new FileInputStream(file);
            int len;
            byte[] buf = new byte[1024 * 10];
            while ((len=fis.read()) != -1){
                out.write(buf,0,len);
            }
            System.out.println("响应发送完毕！");


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
